import java.net.*;
import java.io.*;

public class WheelsServer {
	private static final int SOCKET_PORT = 44444;
	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		ServerSocket serverSocket = null;
		try {
			serverSocket = new ServerSocket(SOCKET_PORT);
		} catch (IOException e){
			System.err.println("Could not listen on port: "+SOCKET_PORT);
			System.exit(1);
		}
		
		Socket clientSocket = null;
		try {
			clientSocket = serverSocket.accept();
		} catch(IOException e) {
			System.err.println("Accept failed on port: "+SOCKET_PORT);
			System.exit(1);
		}
		
		System.out.println("Server started listening");
		
		ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
		CounterInputStream counterInputStream = new CounterInputStream(clientSocket.getInputStream());
		ObjectInputStream in = new ObjectInputStream(counterInputStream);
		ContainerResumeExt containerExt = null;

		
		ContainerResumeSer containerSer = null;
		try {
			containerExt = (ContainerResumeExt) in.readObject();
			System.out.println("Server received "+containerExt.toString()+" and " + counterInputStream.getBytesRead() + " bytes");
			counterInputStream.resetCounter();
			containerSer = (ContainerResumeSer) in.readObject();
			System.out.println("Server received "+containerSer.toString()+" and " + counterInputStream.getBytesRead() + " bytes, then closed");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		out.close();
		in.close();
		clientSocket.close();
		serverSocket.close();

	}

}
